package cn.donting.jme.handle;

import cn.donting.jme.EditorContent;
import cn.donting.jme.core.state.SceneToolState;
import cn.donting.jme.core.state.ShowCameraMatrixViewState;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.editor.scene.SceneCamera;
import cn.donting.jme.event.EditorCloseEvent;
import cn.donting.jme.event.SelectEvent;
import cn.donting.jme.event.file.OpenSceneEvent;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.inspect.InspectBuilder;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.swing.tab.InspectTab;
import cn.donting.jme.swing.tab.scene.SceneTreeTab;
import cn.donting.jme.swing.tab.SceneViewTab;
import cn.donting.jme.utile.SaveUtil;
import com.google.common.eventbus.Subscribe;
import com.jme3.scene.Spatial;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Bean
@Slf4j
@EvenHandle
public class JmeEventHandle {

    private Scene scene;
    private File file;

    @Subscribe
    public void openSceneEvent(OpenSceneEvent openSceneEvent) throws IOException {
        Scene scene = openSceneEvent.getScene();
        SceneTreeTab sceneTreeTab = EditorContent.getEditorUI().getTabOfAdd(SceneTreeTab.TAB_INDEX, SceneTreeTab.class);
        SceneViewTab sceneViewTab = EditorContent.getEditorUI().getTabOfAdd(SceneViewTab.TAB_INDEX, SceneViewTab.class);
        if (this.scene != null) {
            SaveUtil.save(this.file, this.scene);
        }
        this.scene = scene;
        this.file = openSceneEvent.getFile();
        EditorContent.setScene(scene);
        EditorContent.runSwing(() -> {
            sceneTreeTab.openScene(scene, openSceneEvent.getFile());
            sceneViewTab.openScene(scene, openSceneEvent.getFile());
        });
    }

    @Subscribe
    public void closeEvent(EditorCloseEvent closeEvent) throws IOException {
        if (this.scene != null) {
            SaveUtil.save(this.file, this.scene);
        }
    }

    @Subscribe
    public void onSelectEvent(SelectEvent selectEvent) throws IOException {
        Object select = selectEvent.getSelect();
        InspectTab inspectTab = EditorContent.getEditorUI().getTabOfAdd(1, InspectTab.class);
        if (select == null) {
            inspectTab.setAttributeGroup(null);
            return;
        }
        SceneToolState sceneToolState = EditorContent.getJmeApplication().getStateManager().getState(SceneToolState.class);
        ShowCameraMatrixViewState showCameraMatrixViewState = EditorContent.getJmeApplication().getStateManager().getState(ShowCameraMatrixViewState.class);
        inspectTab.setLoading();
        EditorContent.submitTask(() -> {
            Inspect inspect = InspectBuilder.create(select);
            EditorContent.runSwing(() -> inspectTab.setAttributeGroup(inspect));
        }, ex -> {
            inspectTab.setLoadError(ex);
        });
        if (select instanceof Spatial) {
            Spatial spatial = (Spatial) select;
            log.info("SelectSpatialEvent:{}", spatial.getName());
            sceneToolState.setSelectSpatial(spatial);
            showCameraMatrixViewState.setSceneCamera(null);
            return;
        }
        if (select instanceof SceneCamera) {
            log.info("SelectSpatialEvent:{}", (((SceneCamera) select).getCamera().getName()));
            showCameraMatrixViewState.setSceneCamera((SceneCamera) select);
            sceneToolState.setSelectCamera((SceneCamera) select);
        }
    }


}
